import {Objects} from './Objects';
import {JSONArray} from './JSON';

export function ArrayList(unique){
    this.unique	=	unique;//如果唯一，该ArrayList就相当于一个Set
    this.arr = [];

    this.add = function(v,index){
        if(this.unique){
            var oldIndex = this.indexOf(v);
            if(oldIndex >= 0){
                this.set(v,index);
                return true;
            }
        }

        if('undefined' === typeof(index)){
            this.arr.push(v);
        }else{
            var size = this.arr.length;
            if(index < 0 || index > size)
                return false;

            if(index === size)
                this.arr.push(v);
            else{
                this.arr = System.arraycopy(this.arr, index, this.arr, index + 1,size - index);
                this.arr[index] = v;
            }
        }
        return true;
    };

    this.push = function(v,index){
        return this.add(v,index);
    };

    this.set = function(v,index){
        if(index < 0)
            return this;
        if(index >= this.arr.length)
            return this;
        this.arr[index] = v;
        return this;
    };

    this.get = function(i){
        if(i < 0 || i >= this.arr.length)
            return null;
        return this.arr[i];
    };

    this.removeObj = function(obj){
        var res = null;
        var arrn = [];
        for(var i = 0 ; i < this.arr.length; i ++){
            try{
                var ti = this.arr[i];
                if((!isNaN(obj) && ti !== obj) || (isNaN(obj) && !obj.equals(ti))){
                    arrn.push(this.arr[i]);
                }
                else{
                    res = this.arr[i];
                }
            }catch(E){

            }
        }
        this.arr = arrn;
        return res;
    };

    this.remove = function(index){
        if(index < 0 || index >= this.arr.length)
            return null;
        var res = this.arr[index];
        this.arr.splice(index,1);
        return res;
    };

    this.toArray = function(){
        return this.arr;
    };

    this.addAll = function(v){
        var len = this.arr.length;
        if(v instanceof ArrayList){
            var array = v.toArray();
            return this.addAllArray(array);
        }
        return len === this.arr.length;
    };

    this.addAllArray = function(v){
        var len = this.arr.length;
        if(v instanceof Array){
            for(var i in v){
                this.add(v[i]);
            }
        }
        return len === this.arr.length;
    };

    this.contains	=	function(obj){
        return this.indexOf(obj) >= 0;
    };

    this.indexOf	=	function(obj){
        if(!obj)
            return false;
        for(var i in this.arr){
            var o = this.arr[i];
            if(o === obj)
                return i;
            try{
                if(o.equals(obj))
                    return i;
            }catch(E){}
            if(Objects.equals(o,obj))
                return i;
        }
        return -1;
    };

    this.clear = function(){
        this.arr = [];
    };

    this.size = function(){
        return this.arr.length;
    };

    this.isEmpty = function(){
        return 0 === this.arr.length;
    };

    this.toJSONArray = function(){
        var arr = new JSONArray();
        return arr.parse(this);
    };
}